package com.mdinic.blogovi.model;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.dod.RooDataOnDemand;
import org.springframework.stereotype.Component;

@Component
@Configurable
@RooDataOnDemand(entity = BlogType.class)
public class BlogTypeDataOnDemand {

    private final Random rnd = new SecureRandom();

    private List<BlogType> data;

    public void setBgColor(BlogType obj, int index) {
        String bgColor = "bgColor_" + index;
        obj.setBgColor(bgColor);
    }

    public void setColor(BlogType obj, int index) {
        String color = "color_" + index;
        obj.setColor(color);
    }

    public BlogType getNewTransientBlogType(int index) {
        BlogType obj = new BlogType();
        setName(obj, index);
        obj.setBgColor("");
        obj.setColor("");
        return obj;
    }

    public void setName(BlogType obj, int index) {
        String name = "name_" + index;
        obj.setName(name);
    }

    public BlogType getSpecificBlogType(int index) {
        init();
        if (index < 0) {
            index = 0;
        }
        if (index > (data.size() - 1)) {
            index = data.size() - 1;
        }
        BlogType obj = data.get(index);
        Long id = obj.getId();
        return BlogType.findBlogType(id);
    }

    public BlogType getRandomBlogType() {
        init();
        BlogType obj = data.get(rnd.nextInt(data.size()));
        Long id = obj.getId();
        return BlogType.findBlogType(id);
    }

    public boolean modifyBlogType(BlogType obj) {
        return false;
    }

    public void init() {
        int from = 0;
        int to = 10;
        data = BlogType.findBlogTypeEntries(from, to);
        if (data == null) {
            throw new IllegalStateException("Find entries implementation for 'BlogType' illegally returned null");
        }
        if (!data.isEmpty()) {
            return;
        }

        data = new ArrayList<BlogType>();
        for (int i = 0; i < 10; i++) {
            BlogType obj = getNewTransientBlogType(i);
            try {
                obj.persist();
            } catch (ConstraintViolationException e) {
                StringBuilder msg = new StringBuilder();
                for (Iterator<ConstraintViolation<?>> iter = e.getConstraintViolations().iterator(); iter.hasNext();) {
                    ConstraintViolation<?> cv = iter.next();
                    msg.append("[").append(cv.getConstraintDescriptor()).append(":").append(cv.getMessage())
                            .append("=").append(cv.getInvalidValue()).append("]");
                }
                throw new RuntimeException(msg.toString(), e);
            }
            obj.flush();
            data.add(obj);
        }
    }
}
